package org.hw.algorithm.number;

import java.util.Comparator;
import java.util.PriorityQueue;

public class MedianMum {
    /*模拟小根堆*/
    private PriorityQueue<Integer> minHeap=new PriorityQueue<Integer>();
    /*模拟大根堆*/
    private PriorityQueue<Integer> maxHeap=new PriorityQueue<Integer>(11,new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o2-o1;
        }
    });

    public void inser(int num){
        if (((minHeap.size()+maxHeap.size())&1)==0){
            if (!maxHeap.isEmpty()&&num<maxHeap.peek()){
                maxHeap.offer(num);
                num=maxHeap.poll();
            }
            minHeap.offer(num);
        }else {
            if (!minHeap.isEmpty()&&num>minHeap.peek()){
                minHeap.offer(num);
                num=minHeap.poll();
            }
            maxHeap.offer(num);
        }
    }
    public int getMedianNum(){
        int mid=-1;
        if (((minHeap.size()+maxHeap.size())&1)==0){
            mid=(minHeap.peek()+maxHeap.peek())/2;
        }else {
            mid=minHeap.peek();
        }
        return mid;
    }
}
